package com.sanji.sjmallmodule.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallmodule.goods.GoodsBean;
import com.sanji.sjmallutils.ImageSizeUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by fhm on 2016/12/23.
 */

public class MainGrideAdapter extends BaseAdapter {
    private List<GoodsBean> mData;
    private Context context;
//    private int itemWidth;
    private int imagWidth;
    RelativeLayout.LayoutParams imgParam;


    public MainGrideAdapter(Context context, List<GoodsBean> mData) {
        this.mData = mData;
        this.context = context;
        //设置图片的大小
//        itemWidth = ImageSizeUtil.setImageWidth(context, 2);
        imagWidth = ImageSizeUtil.setImageWidth(context, 3);
        imgParam = new RelativeLayout.LayoutParams(imagWidth, imagWidth);
        imgParam.addRule(RelativeLayout.CENTER_HORIZONTAL);
    }

    public void notifyAdapter(List<GoodsBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vHolder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_main_grideview_item, null);
            vHolder = new ViewHolder(view);
//            view.setLayoutParams(new AbsListView.LayoutParams(itemWidth, RelativeLayout.LayoutParams.WRAP_CONTENT));
            vHolder.phoneImage.setLayoutParams(imgParam);
            view.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) view.getTag();
        }
        GoodsBean phone = mData.get(i);
        vHolder.phoneName.setText(phone.getPhoneName());
        vHolder.phonePrice.setText(context.getString(R.string.price_title) + phone.getPhonePrice());
//        vHolder.phoneImage.setLayoutParams(imgParams);
        //图片的标记待定
        //vHolder.imageFlag
        Glide.with(context).load(phone.getUrl())
                //使用centerCrop是利用图片图填充ImageView设置的大小，如果ImageView的Height是match_parent则图片就会被拉伸填充
                .centerCrop()
                //当加载网络图片时，由于加载过程中图片未能及时显示，此时可能需要设置等待时的图片，通过placeHolder()方法
                .placeholder(R.mipmap.default_3j_img)
                //当加载图片失败时，通过error(Drawable drawable)方法设置加载失败后的图片显示
                .error(R.mipmap.default_3j_img)
                .crossFade()
                //DiskCacheStrategy.SOURCE：缓存原始数据，DiskCacheStrategy.RESULT：缓存变换(如缩放、裁剪等)后的资源数据，
                // DiskCacheStrategy.NONE：什么都不缓存，DiskCacheStrategy.ALL：缓存SOURC和RESULT。
                // 默认采用DiskCacheStrategy.RESULT策略，对于download only操作要使用DiskCacheStrategy.SOURCE。
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(vHolder.phoneImage);
        vHolder.imageFlag.setVisibility(View.VISIBLE);
        if (phone.getTopFlag() == 0) {
            vHolder.imageFlag.setImageResource(R.mipmap.main_flag_top1);
        } else if (phone.getTopFlag() == 1) {
            vHolder.imageFlag.setImageResource(R.mipmap.main_flag_top2);
        } else if (phone.getTopFlag() == 2) {
            vHolder.imageFlag.setImageResource(R.mipmap.main_flag_top3);
        } else if (phone.getTopFlag() == 3) {
            vHolder.imageFlag.setVisibility(View.GONE);
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.gridview_phone_image)
        ImageView phoneImage;
        @BindView(R.id.gridview_phone_name)
        TextView phoneName;
        @BindView(R.id.gridview_phone_price)
        TextView phonePrice;
        @BindView(R.id.fragment_main_image_flag)
        ImageView imageFlag;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
